<?php 

class SimpleXMLExtended extends SimpleXMLElement 
{ 
	public function addCData($cdata_text)
	{ 
		$node = dom_import_simplexml($this); 
		$no = $node->ownerDocument; 
		$node->appendChild($no->createCDATASection($cdata_text)); 
	} 
}


class MM_Connector
{

	private $xml;
	private $error;
	private $reply;
	private $api_key;
	private $server_address;

	public function __construct($api_key, $server_address, $status_address = null)
	{
		$this->api_key = $api_key;
		$this->server_address = $server_address;

		$this->xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><request/>');

        $this->xml->addAttribute('source', 'MM PHP-client 2.0.3');
		$authentication = $this->xml->addChild('authentication');
		$authentication->addAttribute('apikey', $this->api_key);

		$this->data = $this->xml->addChild('data');

		if(!is_null($status_address))
			$this->data->addChild('statuscallbackurl', $status_address);
	}

	public function addPremiumMessage(MM_Premium_Message $mm_message)
	{

		$message = $this->data->addChild('message');

		$message->addChild('sendername', $mm_message->getSendername());
		$text = $message->addChild('text');

		$text->addCData($mm_message->getMessage());

		foreach($mm_message->getRecipients() as $recipient)
		{
			$recipients = $message->addChild('recipients');
			$recipients->addChild('msisdn', $recipient);
		}

		$overcharge = $message->addChild('overchargeinfo');

		$overcharge->addAttribute('price', $mm_message->getPrice());
		$overcharge->addAttribute('type', $mm_message->getType());
		$overcharge->addAttribute('countrycode', $mm_message->getCountryCode());
		$overcharge->addAttribute('shortcode', $mm_message->getShortcode());

	}

	public function addMessage(MM_Message $mm_message)
	{

		$message = $this->data->addChild('message');

		$message->addChild('sendername', $mm_message->getSendername());
		$text = $message->addChild('text');

		$text->addCData($mm_message->getMessage());

		foreach($mm_message->getRecipients() as $recipient)
		{
			$recipients = $message->addChild('recipients');
			$recipients->addChild('msisdn', $recipient);
		}

	}

	public function send()
	{

		$x = curl_init($this->server_address.'/Api/V2/SendMessages');
		curl_setopt($x, CURLOPT_HEADER, 0);
		curl_setopt($x, CURLOPT_POST, 1);
		curl_setopt($x, CURLOPT_POSTFIELDS, array('xml' => $this->xml->asXML()));
		curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
		$this->reply = curl_exec($x);
		curl_close($x);

		libxml_use_internal_errors(true);

		if(simplexml_load_string($this->reply))
		{
			return true;
		}

		else
		{
			$this->error = $this->reply;
			return false;
		}
	}

	public function addList($msisdn, $name, $group='29f15788-f7eb-4198-beb5-03e9f329ba35')
	{//[insert-api-key][phone-number-incl-countrycode][insert-name]
		$x = curl_init('https://admin.textmanager.com/Api/V3/Recipients/CreateOrUpdate?apikey='.$this->api_key);
		curl_setopt($x, CURLOPT_HEADER, 0);
		curl_setopt($x, CURLOPT_POST, 1);
		curl_setopt($x, CURLOPT_POSTFIELDS, array('groupId' => $group,
											'msisdn' => $msisdn,
											'name' => $name));
		curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
		$status_load = curl_exec($x);
		curl_close($x);

		libxml_use_internal_errors(true);

		if(simplexml_load_string($this->reply))
		{
			return true;
		}

		else
		{
			$this->error = $this->reply;
			return false;
		}
	}

	public function getMessageIds()
	{
		$message_ids = simplexml_load_string($this->reply);

		return $message_ids;
	}

	public function getStatus()
	{
		$x = curl_init('https://'.$this->server_address.'/Api/V2/Get/GetMessageStatus?source=MmPhpClient2_0_3&apiKey='.$this->api_key);
		curl_setopt($x, CURLOPT_HEADER, 0);
		curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
		$status_load = curl_exec($x);
		curl_close($x);

		$status_lines = explode("\n", $status_load);

		$return = array();

		foreach($status_lines as $status_line)
		{
			if($status_line) 
			{
				$params = explode(":", $status_line);

				$return[] = array('id' => $params[0], 'status_code' => $params[1], 'status_description' => $params[2]);
			}
		}

		return $return;

	}

	public function getError()
	{
		return $this->error;
	}

}

class MM_Message
{

	private $message = '';
	private $sendername = '';
	private $recipients = array();

	public function __construct($message, array $recipients, $sendername)
	{
		$this->message = $message;
		$this->recipients = $recipients;
		$this->sendername = $sendername;
	}

	public function getMessage()
	{
		return $this->message;
	}

	public function getRecipients()
	{
		return $this->recipients;
	}

	public function getSendername()
	{
		return $this->sendername;
	}

}

class MM_Premium_Message extends MM_Message
{

	private $price = '';
	private $type = '';
	private $country_code = '';
	private $shortcode = '';

	public function __construct($message, array $recipients, $sendername, $price, $type, $country_code, $shortcode)
	{

		parent::__construct($message, $recipients, $sendername);

		$this->price = $price;
		$this->type = $type;
		$this->country_code = $country_code;
		$this->shortcode = $shortcode;

	}

	public function getPrice()
	{
		return $this->price;
	}

	public function getType()
	{
		return $this->type;
	}

	public function getCountryCode()
	{
		return $this->country_code;
	}

	public function getShortcode()
	{
		return $this->shortcode;
	}

}

?>
